//
//  NSString+ExRegex.m
//  OC-Example
//
//  Created by lcj on 2020/11/3.
//  Copyright © 2020 apang.men. All rights reserved.
//

#import "NSString+ExRegex.h"

@implementation NSString (ExRegex)
- (BOOL)apm_executeRegex:(NSString *)regex {
    //MARK:执行正则
    if (self.length>0) {
        NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [predicate evaluateWithObject:self];
    }
    return NO;
}

- (NSString *)apm_stringWithRegex:(NSString *)regex {
    if (self.length>0 && regex.length>0) {
        NSRegularExpression * regularExpression = [NSRegularExpression regularExpressionWithPattern:regex options:0 error:nil];
        NSString * newString = [regularExpression stringByReplacingMatchesInString:self options:NSMatchingReportCompletion range:NSMakeRange(0, self.length) withTemplate:@""];
        return newString;
    }
    return self;
}

- (BOOL)apm_onlyNumber:(NSString *)range {
    //MARK:是否只包含数字
    range = (range.length>0)?range:@"*";
    NSString * regex = [NSString stringWithFormat:@"[0-9]%@",range];
    return [self apm_executeRegex:regex];
}

- (BOOL)apm_isCNTelPhoneNumber {
    //MARK: 是否是中国手机号码
    //^1[3-9]\d{9}$
    NSString * regex = @"1[3-9]\\d{9}";
    return [self apm_executeRegex:regex];
}

- (BOOL)apm_isEamilAddress {
    //MARK: 电子邮箱地址
    //^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
    NSString * regex = @"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    return [self apm_executeRegex:regex];
}

- (BOOL)apm_upperOrLowercaseLettersOrNumber:(NSString *)range {
    //MARK:大写或小写字母或数字
    range = (range.length>0)?range:@"*";
    NSString * regex = [NSString stringWithFormat:@"[a-zA-Z0-9]%@",range];
    return [self apm_executeRegex:regex];
}

- (BOOL)apm_lettersAndNumber:(NSString *)range {
    //MARK:字母数字组合，不区分大小写
    //^([A-Z0-9]|[a-z0-9]|[a-zA-Z0-9]){8,10}$
    range = (range.length>0)?range:@"*";
    NSString * regex = [NSString stringWithFormat:@"(?=.*\\d)(?=.*[A-Za-z])[a-zA-Z0-9]%@",range];
    return [self apm_executeRegex:regex];
}

- (BOOL)apm_upperAndLowercaseLettersAndNumber:(NSString *)range {
    //MARK:只有大小写字母和数字
    //^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]$
    range = (range.length>0)?range:@"*";
    NSString * regex = [NSString stringWithFormat:@"(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]%@",range];
    return [self apm_executeRegex:regex];
}

+ (BOOL)apm_executeRegex:(NSString *)regex string:(NSString *)string {
    //MARK:执行正则
    return [string apm_executeRegex:string];
}

+ (BOOL)apm_onlyNumber:(NSString *)string range:(NSString *)range {
    //MARK:是否只包含数字
    return [string apm_onlyNumber:range];
}

+ (BOOL)apm_isCNTelPhoneNumber:(NSString *)string {
    //MARK: 是否时中国手机号码
    return [string apm_isCNTelPhoneNumber];
}

+ (BOOL)apm_isEamilAddress:(NSString *)string {
    //MARK: 电子邮箱地址
    return [string apm_isEamilAddress];
}

+ (BOOL)apm_upperOrLowercaseLettersOrNumber:(NSString *)string range:(NSString *)range {
    //MARK:大写或小写字母或数字
    return [string apm_upperOrLowercaseLettersOrNumber:range];
}

+ (BOOL)apm_lettersAndNumber:(NSString *)string range:(NSString *)range {
    return [string apm_lettersAndNumber:range];
}

+ (BOOL)apm_upperAndLowercaseLettersAndNumber:(NSString *)string range:(NSString *)range {
    //MARK:只有大小写字母和数字
    return [string apm_upperAndLowercaseLettersAndNumber:range];
}

@end
